﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.admin.user
{
   
    public partial class userEdite : System.Web.UI.Page
    {
        a_user_dal userdal = new a_user_dal();
        a_user_model usermodel = new a_user_model();
        protected void Page_Load(object sender, EventArgs e)
        {

            switch (Request.QueryString["action"])
            {
                case "code"://判断验证码是否一致
                    string R_verifyCode = Request.Form["code"].ToString().Trim().ToLower();
                    string S_verifyCode = Session["verifyCode"].ToString().Trim().ToLower();

                    if (S_verifyCode == R_verifyCode)
                    {
                        Response.Write("true");
                    }
                    else
                    {
                        Response.Write("false");
                    }
                    Response.End();
                    break;
                case "reg"://注册
                    RegisterUser();
                    break;
            }
        }

        private void RegisterUser()
        {
            string User_Name = Request.Form["name"].Trim();
            string User_Phone = Request.Form["phone"].Trim();
            string User_Email = Request.Form["email"].Trim();
            string User_Pwd = Request.Form["pwd"].Trim();
            string User_QQ = Request.Form["qq"].Trim();
            string R_verifyCode = Request.Form["code"].ToString().Trim().ToLower();
            //string Invite = Request.Form["invite"].ToString().Trim().ToLower();
            string S_verifyCode = Session["verifyCode"].ToString().Trim().ToLower();

            System.Data.DataSet dsXml = new System.Data.DataSet();
            //dsXml.ReadXml(Server.MapPath("/Config/Xml/InviteCode.xml"));
            //string InviteCode = dsXml.Tables[0].Rows[0][0].ToString().Trim();
            dsXml.Clear();
            if (!PageValidate.IsValidInput(ref User_Name, true))
            {
                Response.Write("请输入正确的用户名");
            }
            else if (userdal.GetList("user_name='" + User_Name + "'").Tables[0].Rows.Count > 0)
            {
                Response.Write("用户名已重复");
            }
            else if (string.IsNullOrEmpty(User_Pwd))
            {
                Response.Write("请输入正确的密码");
            }
            else if (!PageValidate.IsNumber(User_Phone) || User_Phone.Length != 11)
            {
                Response.Write("请输入正确的手机号码");
            }
            else if (userdal.GetList("user_phone='" + User_Phone + "'").Tables[0].Rows.Count > 0)
            {
                Response.Write("手机号码已注册");
            }
            else if (!PageValidate.IsEmail(User_Email))
            {
                Response.Write("请输入正确的邮箱");
            }
            else if (userdal.GetList("user_email='" + User_Email + "'").Tables[0].Rows.Count > 0)
            {
                Response.Write("邮箱已注册");
            }
            //else if (string.IsNullOrEmpty(Invite))
            //{
            //    Response.Write("请输入邀请码");
            //}
            else if (Request.Form["invite"] != "junweb")
            {
                Response.Write("请输入正确的邀请码");
            }
            else if (string.IsNullOrEmpty(R_verifyCode))
            {
                Response.Write("请输入正确的验证码");
            }
            else if (!R_verifyCode.Equals(S_verifyCode))
            {
                Response.Write("验证码错误");
            }
            else
            {
                usermodel.user_addtime = DateTime.Now;
                usermodel.user_email = User_Email;
                usermodel.user_name = User_Name;
                usermodel.user_phone = User_Phone;
                usermodel.user_psw = MD5Provider.GetMd5(User_Pwd);
                usermodel.user_qq = User_QQ;
                usermodel.user_lasttime = DateTime.Now;

                string User_ID = userdal.Add(usermodel).ToString();
                if (int.Parse(User_ID) > 0)
                {
                    Response.Write("true");
                }
                else
                {
                    Response.Write("未知错误");
                }
            }
            Response.End();
        }
    }
}